﻿<div ng-controller="Provider_EditC">
    <div class="bg-light lter b-b wrapper-md">
        <h1 class="m-n font-thin h3">编辑供应商信息</h1>
    </div>
    <div class="modal fade" id="tupian" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog modal-lg" role="document">
            <div class="modal-content">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <!--<h4 class="modal-title" id="myModalLabe3">收货凭证 </h4>-->
                <!--<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>-->
                <img style="width: 100%;" ng-src="/file/merchant/{{editReceiptdata}}" alt="用户凭证">
            </div>
        </div>
    </div>
    <div class="modal fade" id="shangchuan" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <form id="myupload" action={{myurl}} method="post" enctype="multipart/form-data">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title" id="myModalLabe2">上传贸易商凭证 </h4>
                    </div>
                    <div class="modal-body">
                        <div class="form-group btn">
                            <!--<input type="label" name="label" id="label" class="form-control" placeholder="请选择文件" /><br />-->
                            <label style="margin-right: 10px;">请选择文件</label>
                            <input type="file" accept=".jpg,.jpeg,.png" name="file1" id="fileupload" class="form-control" value="" /><br />
                        </div>
                        <div class="progress">
                            <span class="bar"></span><span class="percent">0%</span>
                        </div>
                        <!-- 显示已上传的文件名 -->
                        <div class="files"></div>
                        <!-- 显示已上传的图片-->
                        <div class="showimg"></div>
                    </div>
                    <div class="modal-footer">
                        <!--<button type="button" class="btn btn-success" ng-disabled="myform1.$invalid" ng-click="okchange()">确认提交</button>-->
                        <input type="button" class="btn btn-default" ng-click="gosubmit2()" value="提交" />
                        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <div class="modal fade" id="dlg" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel5">人员选择</h4>
                </div>
                <div class="modal-body">
                    <!--<input class="selectFind form-control" type="text" ng-model="searchTxt" placeholder="查找" />-->
                    <div class="input-group ">
                        <input class="selectFind form-control" type="search" ng-model="searchTxt" placeholder="查找">
                        <span class="glyphicon glyphicon-remove input-group-addon" ng-click="empty()"></span>
                    </div>
                    <div class="selectShow" style="width: 100%; height: 200px; border: 1px solid #CCCCCC; overflow: auto;">
                        <div style="cursor: pointer;" class="form-control" ng-click="selectEmp(x , 1)" ng-repeat=" x in userdata|filter:{Name : searchTxt}">{{x.Name}}</div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>

                </div>
            </div>
        </div>
    </div>
    <div class="modal fade" id="dlg1" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel1">人员选择</h4>
                </div>
                <div class="modal-body">
                    <!--<input class="selectFind form-control" type="text" ng-model="searchTxt" placeholder="查找"/>-->
                    <div class="input-group ">
                        <input class="selectFind form-control" type="search" ng-model="searchTxt" placeholder="查找">
                        <span class="glyphicon glyphicon-remove input-group-addon" ng-click="empty()"></span>
                    </div>
                    <div class="selectShow" style="width: 100%; height: 200px; border: 1px solid #CCCCCC; overflow: auto;">
                        <div style="cursor: pointer;" class="form-control" ng-click="selectEmp(x , 2)" ng-repeat=" x in userdata|filter:{Name : searchTxt}">{{x.Name}}</div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>

                </div>
            </div>
        </div>
    </div>

    <div class="modal fade" id="dlgYYRY" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">人员选择</h4>
                </div>
                <div class="modal-body">
                    <div class="input-group ">
                        <input class="selectFind form-control" type="search" ng-model="searchTxt" placeholder="查找">
                        <span class="glyphicon glyphicon-remove input-group-addon" ng-click="empty()"></span>
                    </div>
                    <div class="selectShow" style="width: 100%; height: 200px; border: 1px solid #CCCCCC; overflow: auto;">
                        <div style="cursor: pointer;" class="form-control" ng-click="selectEmp(x , 3)" ng-repeat=" x in userdata|filter:{Name : searchTxt}">{{x.Name}}</div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>

                </div>
            </div>
        </div>
    </div>


    <div class="wrapper-md">
        <div class="panel panel-default">
            <div class="table-responsive">
                <form name="form" class="form-validation">
                    <div class="panel panel-default">
                        <div class="panel-body">
                            <div class="form-group">
                                <label>供应商名称</label>
                                <input ng-model="editdata.Name" type="text" class="form-control" disabled>
                            </div>
                            <div class="form-group">
                                <label>
                                    区域
                                </label>
                                <div class="input-group">
                                    <span class="input-group-addon">省</span>
                                    <select required class="form-control" ng-options="x.FillName for x in districts" ng-model="selectedDistrict1"></select>
                                    <span class="input-group-addon">市</span>
                                    <select required class="form-control" ng-options="x as x.Name for x in selectedDistrict1.Children track by x.Id" ng-model="editdata.District"></select>
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="checkbox">
                                    <label for="feibiao" style="margin-right: 10px; padding: 0px">是否允许非标</label>
                                    <label class="i-checks">
                                        <input type="checkbox" id="feibiao" ng-model="editdata.IsNonStandard">
                                        <i></i>
                                    </label>
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="form-group">
                                    <label>先款优惠</label>
                                    <input ng-model="editdata.FullPaymentDiscount" type="number" min="0" class="form-control" required>
                                </div>
                                <div class="form-group" ng-if="editdata.IsNonStandard">
                                    <label>非标优惠</label>
                                    <input ng-model="editdata.NonstandardDiscount" type="number" min="0" class="form-control" required>
                                </div>
                                <div class="form-group">
                                    <label>区域经理</label>
                                    <!--<select required class="form-control" ng-options="x as x.Name for x in userdata  track by x.Id" ng-model="editdata.Manager"></select>-->
                                    <button class="form-control text-left" ng-model="editdata.Manager.Name" data-toggle="modal" data-target="#dlg" required>
                                        {{editdata.Manager.Name}}
                                    </button>
                                </div>
                                <div class="form-group">
                                    <label>驻厂专员</label>
                                    <!--<select required class="form-control" ng-options="x.Name for x in userdata  track by x.Id" ng-model="editdata.MainFieldMan"></select>-->
                                    <button class="form-control text-left" ng-model="editdata.MainFieldMan.Name" data-toggle="modal" data-target="#dlg1" required>
                                        {{editdata.MainFieldMan.Name}}
                                    </button>
                                </div>
                                <div class="form-group">
                                    <label>运营人员</label>
                                    <!--<select required class="form-control" ng-options="x.Name for x in userdata  track by x.Id" ng-model="editdata.MainFieldMan"></select>-->
                                    <button class="form-control text-left" ng-model="editdata.OperationsMan.Name" data-toggle="modal" data-target="#dlgYYRY" required>
                                        {{editdata.OperationsMan.Name}}
                                    </button>
                                </div>
                                <div class="form-group" ng-if="Imgtitles.length >= 1">
                                    <label>用户凭证：</label>
                                    <img style="height: 50px; margin: 10px" ng-repeat="item in Imgtitles track by $index" ng-src="/file/merchant/{{item.FileName}}" alt="用户凭证" ng-click="editReceipt(item)" data-toggle="modal" data-target="#tupian">
                                </div>
                                <div class="row text-right" style="text-align:center">
                                    <button type="button" class="btn btn-success" ng-if="editdata.ProviderType==2" data-toggle="modal" data-target="#shangchuan">上传贸易商凭证</button>
                                    <span style="padding:0 5%"></span>
                                    <button type="button" class="btn btn-success" ng-disabled="form.$invalid" ng-click="suppsub()">确认修改</button>
                                    <span style="padding:0 5%"></span>
                                    <button type="button" class="btn btn-default" ng-click="abolish()">取消</button>
                                </div>
                            </div>
                    </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<script>
    angular.module('app')//非标优惠
        .controller("Provider_EditC", function ($scope, $state, $http, $stateParams) {
            $scope.selectedDistrict1 = [];
            $scope.istrue = true;
            $scope.myurl = "../api/Upload/MerchantInfo?id=" + $stateParams.id;
            $scope.editdata= [
                District = ""
            ];
            if($stateParams.type == 1){
                $scope.myurl2 = 'app.TraderManage_List';
            }else {
                $scope.myurl2 = 'app.Provider_List';
            }
            $scope.istrue2 = true;
            $scope.istrue3 = true;
//            修改按钮
            $scope.editisture = function(){
                $scope.istrue = false;
            };
            $scope.editisture2 = function(){
                $scope.istrue2 = false;
            };
            $scope.editisture3 = function(){
                $scope.istrue3 = false;
            };
            //请求数据
            $http({
                url: "/api/Provider/" + $stateParams.id,
                method: "GET"
            })
                    .then(function (res) {
                        $scope.editdata = res.data;
                    console.log($scope.editdata.District);
                    //获取地市
                        $scope.districts =JSON.parse(window.localStorage.getItem('districts'));
                        if ($scope.editdata.District) {
                            for (var i = 0; i < $scope.districts.length; i++) {
                                if ($scope.districts[i].Id == $scope.editdata.District.Parent.Id) {
                                    $scope.selectedDistrict1 = $scope.districts[i];
                                    // console.log(selectedDistrict1);
                                    break;
                                }
                            }
                        }
                        $http.get("/api/Upload/GetMerchantInfo",{params:{"id": $stateParams.id}})
                                .then(function (res) {
                                    $scope.Imgtitles = res.data.Data;
                                })
                                .then(function (error) {
                                    if (error) {
                                        alert("凭证获取失败");
                                    }
                                });
                })
                    .then(function (error) {
                        if (error) {
                            console.log("error");
                        }
                    });

            

            //获取人员
            $http({
                url:"../api/Employee/List?name=&page=1&rows=9999",
                method:"GET"
            })
                    .then(function (res) {
                        $scope.userdata = res.data.Data;
                    })
                    .then(function (error) {
                        if(error){
                            console.log("error");
                        }
                    });
//            提交信息
            $scope.suppsub = function(){
                $http.put('../api/Provider', $scope.editdata)
                        .success(function(res){
                            console.log(res);
                            alert("修改成功");
                            $state.go($scope.myurl2);
                        });
            };
            //取消更改
            $scope.abolish = function(){
                $state.go($scope.myurl2);
            };
            $scope.selectEmp= function(emp , type) {
                if(type == 1){
                    $scope.editdata.Manager = emp;
                    console.log($scope.editdata.Manager);
                    $('#dlg').modal("hide");
                }else if (type == 2){
                    $scope.editdata.MainFieldMan = emp;
                    console.log($scope.editdata.MainFieldMan);
                    $('#dlg1').modal("hide");
                }
                else if (type == 3) {
                    $scope.editdata.OperationsMan = emp;
                    $('#dlgYYRY').modal("hide");
                }
            };
            //查看大图
            $scope.editReceipt = function(item){
                $scope.editReceiptdata = item.FileName;
            };
            $scope.bar = $(".bar");//进度条
            $scope.percent = $('.percent');//获取上传百分比
            $scope.showimg = $('.showimg');//显示图片的div
            $scope.progress = $('.progress');//显示进度的div
            $scope.files = $('.files');//文件上传控件的input元素
            $scope.btn = $('.btn span'); //按钮文本
            $scope.gosubmit2 = function (){
                var str = $("#fileupload").val();
                if((str==null)||(str.length==0)){
                    alert("请选择文件后提交！");
                    return false;
                }
                    var exName = "JPG,GIF,JPEG,PNG";
                    var back_name = str.substring(str.length-3,str.length);
                    if(exName.indexOf(back_name.toUpperCase())==-1){
                        alert('请选择正确格式喔!');return false;
                    }
                    var input = document.getElementById("fileupload");
                    if(input.files){
                        //读取图片数据
                        var f = input.files[0];
                        var reader = new FileReader();
                        reader.onload = function (e) {
                            var data = e.target.result;
                            //加载图片获取图片真实宽度和高度
                            var image = new Image();
                            image.onload=function(){
                                var width = image.width;
                                var height = image.height;
                                if(width>0 && height>0){
                                    if(f.size>57*1024){alert('当前图片已经超大!大小为'+Math.ceil(f.size/1024)+'K');return false;}
                                    if(width>1000){alert('要求宽度1000px!当前图片宽为'+width);return false;}
                                    if(height>1000){alert('已经高度1000px!当前图片高为'+height);return false;}
                                    alert('恭喜!上传成功,当前图片宽为'+width+'高为'+height+'大小为'+(Math.ceil(f.size/1024))+'K');
                                }
                                $("#myupload").ajaxSubmit({
                                    dataType :'json',//返回数据类型
                                    beforeSend:function(){
                                        $scope.showimg.empty();
                                        $scope.progress.show();
                                        $scope.percentVal = '0%';
                                        $scope.bar.width($scope.percentVal);
                                        $scope.percent.html($scope.percentVal);
                                        $scope.btn.html('上传中..');
                                    },
                                    //更新进度条事件处理代码
                                    uploadProgress:function(event,position,total,percentComplete){
                                        $scope.percentVal = percentComplete + '%';
                                        $scope.bar.width($scope.percentVal);
                                        $scope.percent.html($scope.percentVal);
                                    },
                                    success:function(data){//图片上传成功时
                                        if(data){
                                            alert("上传成功");
                                            $('#shangchuan').modal("hide");
                                            $state.go("app.Provider_Edit", {
                                                id : $stateParams.id,
                                                type : $stateParams.type
                                            });
                                        }
                                    },
                                    error:function(xhr){
                                        $scope.btn.html('上传失败');
                                        $scope.bar.width('0');
                                        $scope.files.html(xhr.responseText);
                                    }
                                });
                            };
                            image.src= data;
                        };
                        reader.readAsDataURL(f);
                    }
            };
        })

</script>